package com.lei.springbootpractice.config;

import com.lei.springbootpractice.dao.CategoryDao;
import com.lei.springbootpractice.dao.ProductsDao;
import com.lei.springbootpractice.pojo.CategoryPojo;
import com.lei.springbootpractice.pojo.ProductsPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private ProductsDao productsDao;

    @Override
    public void run(String... args) {
        // 不再自动初始化商品分类，改为通过管理界面手动添加
        initCategories();
        
        // 初始化测试商品（仅在已有分类的情况下）
        initTestProducts();
        
        System.out.println("数据初始化完成 - 分类和商品数据请通过管理界面添加");
    }

    // 已弃用 - 分类数据现在通过管理界面添加

    private void initCategories() {
        // 电子产品分类
        CategoryPojo electronics = new CategoryPojo();
        electronics.setCategoryId("1");
        electronics.setCategoryName("电子产品");
        categoryDao.insert(electronics);

        // 电子产品的子分类
        CategoryPojo laptop = new CategoryPojo();
        laptop.setCategoryId("1-1");
        laptop.setCategoryName("笔记本电脑");
        laptop.setParentCategoryId("1");
        categoryDao.insert(laptop);

        CategoryPojo phone = new CategoryPojo();
        phone.setCategoryId("1-2");
        phone.setCategoryName("手机");
        phone.setParentCategoryId("1");
        categoryDao.insert(phone);

        CategoryPojo tablet = new CategoryPojo();
        tablet.setCategoryId("1-3");
        tablet.setCategoryName("平板电脑");
        tablet.setParentCategoryId("1");
        categoryDao.insert(tablet);

        // 服装分类
        CategoryPojo clothing = new CategoryPojo();
        clothing.setCategoryId("2");
        clothing.setCategoryName("服装");
        categoryDao.insert(clothing);

        // 服装子分类
        CategoryPojo menClothing = new CategoryPojo();
        menClothing.setCategoryId("2-1");
        menClothing.setCategoryName("男装");
        menClothing.setParentCategoryId("2");
        categoryDao.insert(menClothing);

        CategoryPojo womenClothing = new CategoryPojo();
        womenClothing.setCategoryId("2-2");
        womenClothing.setCategoryName("女装");
        womenClothing.setParentCategoryId("2");
        categoryDao.insert(womenClothing);

        CategoryPojo childrenClothing = new CategoryPojo();
        childrenClothing.setCategoryId("2-3");
        childrenClothing.setCategoryName("童装");
        childrenClothing.setParentCategoryId("2");
        categoryDao.insert(childrenClothing);

        // 食品分类
        CategoryPojo food = new CategoryPojo();
        food.setCategoryId("3");
        food.setCategoryName("食品");
        categoryDao.insert(food);

        // 食品子分类
        CategoryPojo snacks = new CategoryPojo();
        snacks.setCategoryId("3-1");
        snacks.setCategoryName("零食");
        snacks.setParentCategoryId("3");
        categoryDao.insert(snacks);

        CategoryPojo drinks = new CategoryPojo();
        drinks.setCategoryId("3-2");
        drinks.setCategoryName("饮料");
        drinks.setParentCategoryId("3");
        categoryDao.insert(drinks);

        CategoryPojo freshFood = new CategoryPojo();
        freshFood.setCategoryId("3-3");
        freshFood.setCategoryName("生鲜");
        freshFood.setParentCategoryId("3");
        categoryDao.insert(freshFood);

        // 图书分类
        CategoryPojo books = new CategoryPojo();
        books.setCategoryId("4");
        books.setCategoryName("图书");
        categoryDao.insert(books);

        // 图书子分类
        CategoryPojo literature = new CategoryPojo();
        literature.setCategoryId("4-1");
        literature.setCategoryName("文学");
        literature.setParentCategoryId("4");
        categoryDao.insert(literature);

        CategoryPojo education = new CategoryPojo();
        education.setCategoryId("4-2");
        education.setCategoryName("教育");
        education.setParentCategoryId("4");
        categoryDao.insert(education);

        CategoryPojo technology = new CategoryPojo();
        technology.setCategoryId("4-3");
        technology.setCategoryName("科技");
        technology.setParentCategoryId("4");
        categoryDao.insert(technology);
    }


    // 已弃用 - 测试商品数据现在通过管理界面添加
    
    private void initTestProducts() {
        // 笔记本电脑商品
        ProductsPojo laptop1 = new ProductsPojo();
        laptop1.setProductId("1001");
        laptop1.setCategoryId("1-1");
        laptop1.setProductName("高性能游戏笔记本");
        laptop1.setPrice(8999.00);
        laptop1.setDescription("16GB内存, 1TB SSD, RTX 3060显卡");
        laptop1.setPhoto("/images/laptop1.jpg");
        laptop1.setRest(15);
        productsDao.insert(laptop1);

    }
    
}